** Just pick out drug deaths data. 
** These are the deaths that Chris Ruhm looks at.

local isolate = 1
local combine = 1

if `isolate' == 1 {
	forvalues yy = 4(1)14 {
		if `yy' <=9 {
			quietly use ..\Mort200`yy'\rawish200`yy'.dta, clear

			** want to mark the correct ICD 10 codes for keeping
			quietly gen keeper = 0
			forvalues i = 1(1)20 {
				display "You are on condition `i' in year `yy'"
				quietly {
					gen num = regexs(1) if regexm(cond`i',"([0-9]+)")
					gen let = regexs(1) if regexm(cond`i',"([A-Z]+)")
					destring num, replace
					replace keeper = 1 if (let=="X" & (inrange(num,40,44) | inrange(num,60,64) | ///
					num==85 ) )
					replace keeper = 1 if (let=="Y" & (inrange(num,10,14) | num==352) )
					replace keeper = 1 if cond`i'=="U016" | cond`i'=="U017"
					replace keeper = 1 if (let=="T" & inrange(num,360,514) )
					drop num let
				}
			}
			quietly keep if keeper==1
			quietly compress
			quietly save ..\processed_data\drugpoisonings`yy'.dta, replace
		}
		else if `yy' >=10 {
			quietly use ..\Mort20`yy'\rawish20`yy'.dta, clear

			** want to mark the correct ICD 10 codes for keeping
			quietly gen keeper = 0
			forvalues i = 1(1)20 {
				display "You are on condition `i' in year `yy'"
				quietly {
					gen num = regexs(1) if regexm(cond`i',"([0-9]+)")
					gen let = regexs(1) if regexm(cond`i',"([A-Z]+)")
					destring num, replace
					replace keeper = 1 if (let=="X" & (inrange(num,40,44) | inrange(num,60,64) | ///
					num==85 ) )
					replace keeper = 1 if (let=="Y" & (inrange(num,10,14) | num==352) )
					replace keeper = 1 if cond`i'=="U016" | cond`i'=="U017"
					replace keeper = 1 if (let=="T" & inrange(num,360,514) )
					drop num let
				}
			}
			quietly keep if keeper==1
			quietly compress
			quietly save ..\processed_data\drugpoisonings`yy'.dta, replace
		}
	}
}

if `combine' == 1 {
	use ..\processed_data\drugpoisonings4.dta, clear
	forvalues i = 5(1)14 {
		if `i'<10 {
			append using ..\processed_data\drugpoisonings`i'.dta
		}
		else {
			append using ..\processed_data\drugpoisonings`i'.dta
		}
	}
	drop keeper
	compress
	save ..\processed_data\drug_poisonings_allyears.dta, replace
}
	
